<?php
session_start();
session_regenerate_id(true);
require_once("include/db.php");

if ($_SERVER['REQUEST_METHOD'] === "POST")
{
    if ($_POST['email'] == '')
    {
        $error = "Please input your email.";
    } elseif ($_POST['name'] == '')
    {
        $error = "Please input your name.";
    } elseif ($_POST['password'] !== $_POST['password2'])
    {
        $error = 'Password inconsistent.';
    } else
    {
        $id = add_user($_POST['email'], $_POST['name'], $_POST['password'], $_POST['question'], $_POST['answer']);
        if ($id === false)
        {
            $error = 'Error adding user. Your email might be used by others.';
        } else
        {
            $_SESSION['user_id'] = $id;
            $_SESSION['user_name'] = $_POST['name'];
            $_SESSION['user_email'] = $_POST['email'];
            header("HTTP/1.1 302 Moved Temporarily");
            header("Location: ./");
            exit();
        }
    }
}
$title = "Register";
include('include/header.php');
?>

<!-- Begin page content -->
<div class="container">
    <div class="page-header">
        <h1>Register</h1>
    </div>
    <p style="color:red"><?php echo @$error; ?></p>
    <form role="form" method="POST">
        <div class="form-group">
            <div class="row">
                <label class="col-md-2 col-md-offset-3 control-label"><h4>Email</h4></label>
                <div class="col-md-4">
                    <input name="email" type="email" class="form-control" id="mail" placeholder="Email" value="<?php echo @$_POST['email']; ?>">
                </div>
            </div>
        </div>

        <div class="form-group">
            <div class="row">
                <label class="col-md-2 col-md-offset-3 control-label"><h4>Name</h4></label>
                <div class="col-md-4">
                    <input name="name" type="text" class="form-control" id="name" placeholder="Name" value="<?php echo @$_POST['name']; ?>">
                </div>
            </div>
        </div>

        <div class="form-group">
            <div class="row">
                <label class="col-md-2 col-md-offset-3 control-label"><h4>Password</h4></label>
                <div class="col-md-4">
                    <input name="password" type="password" class="form-control" id="password" placeholder="Password">
                </div>
            </div>
        </div>

        <div class="form-group">
            <div class="row">
                <label class="col-md-2 col-md-offset-3 control-label"><h4>Confirm Password</h4></label>
                <div class="col-md-4">
                    <input name="password2" type="password" class="form-control" id="password2" placeholder="Input password again">
                </div>
            </div>
        </div>

        <div class="form-group">
            <div class="row">
                <label class="col-md-2 col-md-offset-3 control-label"><h4>Security Question</h4></label>
                <div class="col-md-4">
                    <select name="question-select" id="question-select" class="form-control">
                        <option value="Q1" selected="selected">When is your birthday?</option>
                        <option value="Q2">What's your first pet name?</option>
                        <option value="Q3">Which team is your favourite?</option>
                        <option value="Q4">Which city do you like best?</option>
                        <option value="Q5">When is your mother's birthday?</option>
                        <option value="Q6">Where do you want to live?</option>
                        <option value="other">Others</option>
                    </select>
                </div>
            </div>
            <div class="row">
                <div class="col-md-4 col-md-offset-5">
                    <input name="question" type="text" class="form-control" id="question" placeholder="If you choose 'others', input your own question here.">
                </div>
            </div>
        </div>

        <div class="form-group">
            <div class="row">
                <label class="col-md-2 col-md-offset-3 control-label"><h4>Security Answer</h4></label>
                <div class="col-md-4">
                    <input name="answer" type="text" class="form-control" id="answer" placeholder="Input your answer for the security question.">
                </div>
            </div>
        </div>

        <div class="form-group">
            <div class="row">
                <div class="col-md-offset-5 col-md-4">
                    <button class="btn btn-primary btn-block" id="submit">Register</button>
                </div>
            </div>
        </div>

    </form>
</div>

<script type="text/javascript">
$("#submit").click(function() {
    if ($("#question-select").val() != "other") {
        $("#question").val($("#question-select :selected").html());
        $("form").submit();
    }
});
</script>

<?php include('include/footer.php'); ?>
